/**
 * @description  杠杆控制
 * @author zls
 */
import React, { PureComponent } from 'react';
import { connect } from 'dva';
import { Spin } from 'antd';
import { SecondHeaderC, TableC, FirstHeaderC } from '../../../comps';
import ketou from '../../../assets/busi/reportingServices/tick_icon.png';
import buketou from '../../../assets/busi/reportingServices/cross_icon.png';
import Style from './style.less';

class leverControl extends PureComponent {
  constructor() {
    super();
    this.state = {
      data: {},
    };
  }

  componentDidMount() {
    const { searchData } = this.props;
    this.getData(searchData);
  }

  UNSAFE_componentWillReceiveProps(nextProps) {
    const { searchData } = nextProps;

    if (JSON.stringify(searchData) !== JSON.stringify(this.props.searchData)) {
      this.getData(searchData);
    }
  }

  //获取数据
  getData = (payload) => {
    this.props.dispatch({
      type: 'commercialBankReportM/getLeverageRatio',
      payload: {
        ...payload,
      },
      callback: (data) => {
        if (data) {
          this.setState({
            data: data[0],
          });
        } else {
          this.setState({
            data: {},
          });
        }
      },
    });
  };

  render() {
    const { data } = this.state;
    const { loading, commercialBankReportM } = this.props;
    const { openModeList } = commercialBankReportM;

    // 和现金报告那不一样，这里是1禁止0通过
    const iconInfo = {
      1: {
        className: 'prohibit',
        img: buketou,
        name: '禁止',
      },
      0: {
        className: 'adopt',
        img: ketou,
        name: '通过',
      },
    };
    return (
      <>
        <div name={'syyh-pdf'} style={{ marginBottom: '16px' }}>
          <FirstHeaderC title='杠杆控制' bottomHeight={false} />
        </div>

        {/* <Spin spinning={this.props.loading == true}> */}
        {data.msg && (
          <div name={'syyh-pdf'} style={{ marginBottom: '16px' }}>
            <SecondHeaderC
              title={data.msg}
              bottomHeight={false}
              Left={
                <div className={Style[iconInfo[data.state]?.className]} style={{ marginLeft: '20px' }}>
                  <img src={iconInfo[data.state]?.img} />
                  {iconInfo[data.state]?.name}
                </div>
              }
            />
          </div>
        )}
        {(data.dataSource?.length != 0 || loading) && (
          <div name={'syyh-pdf'} isTable='true' style={{ marginBottom: '16px' }}>
            <TableC
              columns={
                data?.columns
                  ?.map((x) => {
                    if (x.key == 'productName') {
                      return {
                        ...x,
                        width: '500px',
                      };
                    }
                    if (x.key == 'openMode') {
                      return {
                        ...x,
                        render: (text) => openModeList?.filter((r) => r.code == text)[0]?.value || '--',
                      };
                    }
                    if (x.key == 'zzc' || x.key == 'jzc') {
                      return {
                        ...x,
                        type: 'money',
                      };
                    }
                    if (x.key == 'leverageRatio') {
                      return {
                        ...x,
                        align: 'right',
                      };
                    }
                    return x;
                  })
                  ?.concat([{ dataIndex: 'konglie', title: '', key: 'konglie', render: () => '' }]) //添加空列，解决表头导出的样式问题
              }
              dataSource={data.dataSource}
              hidePage={true}
              rowKey={'id'}
              loading={loading}
            ></TableC>
          </div>
        )}
        {/* </Spin> */}
      </>
    );
  }
}
export default connect(({ commercialBankReportM, loading }) => ({
  commercialBankReportM,
  loading: loading.effects['commercialBankReportM/getLeverageRatio'],
}))(leverControl);
